﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using wenku8.Parser;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace wenku8.Xamls
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class SignInPage : Page
    {
        public SignInPage()
        {
            this.InitializeComponent();
        }


        private void SignInButton_Click(object sender, RoutedEventArgs e)
        {
            if (userNameTextBox.Text == "" || passwordPasswordBox.Password == "")
                return;
            ApplicationDataContainer appData = ApplicationData.Current.LocalSettings;
            appData.Values.Add("SavePassword", savePasswordCheckBox.IsChecked);
            if (!appData.Values.ContainsKey("UserName")) {
                appData.Values.Add("UserName", userNameTextBox.Text);
            }
            if (!appData.Values.ContainsKey("Password")) {
                appData.Values.Add("Password", passwordPasswordBox.Password);
            }
            signInGrid.Visibility = Visibility.Collapsed;
            try {
                HTMLParser.InitializeAsync();
                this.Frame.Navigate(typeof(BookItemsPage), new Button { Name = "updatedNovelsButton" });
            }
            catch (Exception) {
                MainPage.PopMessageDialog("网络或服务器故障！");
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e) => MainPage.DisableProgressRing();
    }
}
